Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation documentation #132

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Add validation documentation #132

wants to merge 3 commits into from

Conversation

askvortsov1
Copy link
Member

No description provided.

@dsevillamartin
Copy link
Member

Without actually reading this, 3 out of the 4 code blocks have a 2-space indentation. This should be changed to 4 spaces for consistency & better readability.

Copy link
Member

@clarkwinkelmann clarkwinkelmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Globally ok. Few notes.

Also, I recognize that validator extender example 😛

docs/extend/validation.md Outdated Show resolved Hide resolved
docs/extend/validation.md Outdated Show resolved Hide resolved
@askvortsov1
Copy link
Member Author

Also, I recognize that validator extender example

First thing I did when trying to revamp documentation was look through your recent Discuss posts 🙇

Oh, and that Eloquent stuff you corrected me on just now is going right in there too 😀

- `$instance->getChanges()` will get all attributes that were modified and are now saved
- `$instance->getOriginal()` will get all attribute values that were retrieved from the database

Also, keep in mind that it's generally preferable to validate data before pushing it into the model instance.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to include a small example here. Maybe:

<?php

use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class MyController implements RequestHandlerInterface
{
    protected $validator;
    public function __construct(UserValidator $validator) {
        $this->validator = $validator;
    }

    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        $attributes = Arr::get($request->getParsedBody(), 'data.attributes', []);

        $this->validator->assertValid($attributes);

        // Do something with the attributes
        // Access them with for example Arr::get($attributes, 'email')
    }
}

Also worth noting somehow:

When validating an array, only keys that are present in the validator will be checked. Other will be left as-it. You should only read values from the array that were validated. To get an array of values that ran through the validator, you can use Arr::only($attributes, array_keys($validator->getRules())).

A validator based on Flarum's AbstractValidator will only validate keys which are present in the $data parameter. This means required rules won't be checked if the data is absent. This is designed to handle API requests where each attribute will be handled separately and ignored when absent. To validate all data including missing data, you need to use a Laravel validator.

@askvortsov1 askvortsov1 linked an issue Mar 10, 2021 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document validation
3 participants